import { Drawer, configProvider } from 'ant-design-vue'
import zhCN from 'ant-design-vue/lib/locale-provider/zh_CN'
import store from '@/store'
// import router from '@/router'
let timeFn = new Date().getTime(); let dialogInstance
let allDialogInstance = []
export default (Vue) => {
  function drawerModal (component, modalProps, componentProps) {
    const _vm = this
    const FLAHISVUE = typeof component === 'string'
    modalProps = Object.assign({
      width: 600,
      maskClosable: false,
      centered: true,
      destroyOnClose: true,
      clickTime: 600,
      dialogClass: 'dragwrawer dragBoxAifan' + Math.random().toString(36).substring(2)
    }, modalProps || {})
    if (!_vm || !_vm._isVue) {
      return
    }
    let dialogDiv = document.querySelector('body>div[type=drawer]')
    if (!dialogDiv) {
      dialogDiv = document.createElement('div')
      dialogDiv.setAttribute('type', 'drawer')
      document.body.appendChild(dialogDiv)
    }
    const handle = function (dialogCompoents, checkFunction, afterHandel) {
      if (checkFunction instanceof Function) {
        const res = checkFunction(dialogCompoents)
        if (res instanceof Promise) {
          res.then(c => {
            c && afterHandel()
          })
        } else {
          res && afterHandel()
        }
      } else {
        afterHandel()
      }
    }

    let componentObj = {
      // router,
      store,
      data () {
        return {
          visible: true,
          dragBox: 'dragBox'
        }
      },
      mounted () {
      },
      methods: {
        handleClose () {
          let modalCallback = modalProps.ons
          handle((modalCallback && modalCallback.onCancel) || (this.$refs._componen && this.$refs._component.onCancel), () => {
            this.selfClose()
          })
        },
        handleOk () {
          let modalCallback = modalProps.ons
          if (new Date().getTime() - timeFn > (modalProps.clickTime)) {
            // 重新赋值时间
            timeFn = new Date().getTime()
            // 满足条件，执行函数
            handle((modalCallback && modalCallback.onOK) || (this.$refs._component && (this.$refs._component.onOK || this.$refs._component.onOk)), () => {
              this.selfClose()
            })
          }
        },
        // 全部关闭弹窗
        modalAllClose () {
          let len = allDialogInstance.length
          while (len > 0) {
            len--
            allDialogInstance[len].visible = false
            // 动画执行完成销毁
            setTimeout(() => {
              allDialogInstance[len].$destroy()
              allDialogInstance.splice(len, 1)
            }, 300)
          }
        },
        // 单独关闭
        selfClose () {
          this.visible = false
          setTimeout(() => {
            this.$destroy()
          }, 300)
        }
      },
      render: function (h) {
        const that = this
        const modalModel = modalProps && modalProps.model
        if (modalModel) {
          delete modalProps.model
        }
        // eslint-disable-next-line no-mixed-operators
        const ModalProps = Object.assign({}, modalModel && { model: modalModel } || {}, {
          ref: '_aifanDrawer',
          attrs: Object.assign({
            title: '操作'
          }, {
            ...(modalProps.attrs || modalProps)
          }, {
            visible: this.visible
          }),
          $slots: {},
          on: Object.assign({}, {
            close: () => {
              that.handleClose()
            }
          })
        })
        const componentModel = componentProps && componentProps.model
        if (componentModel) {
          delete componentProps.model
        }
        componentProps = { ...componentProps }
        // eslint-disable-next-line no-mixed-operators
        const ComponentProps = Object.assign({}, componentModel && { model: componentModel } || {}, {
          ref: '_component',
          attrs: Object.assign({}, {
            ...((componentProps && componentProps.attrs) || componentProps),
            selfClose: () => {
              this.selfClose()
            },
            modalAllClose: () => {
              this.modalAllClose()
            }
          }),
          on: Object.assign({}, {
            ...((componentProps && componentProps.on) || componentProps)
          })
        })
        const configProps = Object.assign({}, {
          attrs: {
            locale: zhCN
          }
        })
        let myComs = {}
        if (FLAHISVUE) {
          var res = Vue.compile(component)
          myComs = {
            render: res.render,
            staticRenderFns: res.staticRenderFns
          }
        }
        // eslint-disable-next-line no-unreachable
        let ELEMENT = FLAHISVUE ? h(Drawer, ModalProps, [h(myComs)]) : h(Drawer, ModalProps, [h(configProvider, configProps, [h(component, ComponentProps)])])
        return ELEMENT
      }
    }

    var MyComponent = Vue.extend(componentObj)
    dialogInstance = new MyComponent().$mount(dialogDiv)
    allDialogInstance.push(dialogInstance)
  }
  Object.defineProperty(Vue.prototype, '$drawer', {
    get: () => {
      return function () {
        drawerModal.apply(this, arguments)
      }
    }
  })
}
